

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.FaceGroup;
import com.loong.loong.hik.service.FaceGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 人脸分组
 *
 * @author jinyiang
 * @date 2023-03-28 15:40:49
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("face/facegroup" )
@Api(value = "facegroup", tags = "人脸分组管理")
public class FaceGroupController {

    private final  FaceGroupService faceGroupService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param faceGroup 人脸分组
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_facegroup_view')" )
    public R getFaceGroupPage(Page page, FaceGroup faceGroup) {


		return R.ok(faceGroupService.page(page,
				Wrappers.query(faceGroup).lambda()
						.like(StringUtils.isNotEmpty(faceGroup.getLikeName()), FaceGroup::getName,faceGroup.getLikeName())
						.between(StringUtils.isNotEmpty(faceGroup.getStartTime())&&StringUtils.isNotEmpty(faceGroup.getEndTime()),FaceGroup::getCreateTime,faceGroup.getStartTime(),faceGroup.getEndTime())));
    }

	/**
	 * 查询list
	 * @param faceGroup 人脸分组
	 * @return
	 */
	@ApiOperation(value = "查询list", notes = "查询list")
	@GetMapping("/list" )
	public R getFaceGroupList( FaceGroup faceGroup) {


		return R.ok(faceGroupService.list(
				Wrappers.query(faceGroup).lambda()
						.like(StringUtils.isNotEmpty(faceGroup.getLikeName()), FaceGroup::getName,faceGroup.getLikeName())
						.between(StringUtils.isNotEmpty(faceGroup.getStartTime())&&StringUtils.isNotEmpty(faceGroup.getEndTime()),FaceGroup::getCreateTime,faceGroup.getStartTime(),faceGroup.getEndTime())));
	}


    /**
     * 通过id查询人脸分组
     * @param indexCode id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_facegroup_view')" )
    public R getById(@PathVariable("id" ) String indexCode) {
        return R.ok(faceGroupService.getById(indexCode));
    }

    /**
     * 新增人脸分组
     * @param faceGroup 人脸分组
     * @return R
     */
    @ApiOperation(value = "新增人脸分组", notes = "新增人脸分组")
    @SysLog("新增人脸分组" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_facegroup_add')" )
    public R save(@RequestBody FaceGroup faceGroup) {

		return faceGroupService.add(faceGroup);
    }

    /**
     * 修改人脸分组
     * @param faceGroup 人脸分组
     * @return R
     */
    @ApiOperation(value = "修改人脸分组", notes = "修改人脸分组")
    @SysLog("修改人脸分组" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_facegroup_edit')" )
    public R updateById(@RequestBody FaceGroup faceGroup) {
        return R.ok(faceGroupService.update(faceGroup));
    }

    /**
     * 通过id删除人脸分组
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过id删除人脸分组", notes = "通过id删除人脸分组")
    @SysLog("通过id删除人脸分组" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_facegroup_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(faceGroupService.del(id));
    }


    /**
     * 导出excel 表格
     * @param faceGroup 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_facegroup_export')" )
    public List<FaceGroup> export(FaceGroup faceGroup) {
		return faceGroupService.list(
				Wrappers.query(faceGroup).lambda()
						.like(StringUtils.isNotEmpty(faceGroup.getLikeName()), FaceGroup::getName,faceGroup.getLikeName())
						.between(StringUtils.isNotEmpty(faceGroup.getStartTime())&&StringUtils.isNotEmpty(faceGroup.getEndTime()),FaceGroup::getCreateTime,faceGroup.getStartTime(),faceGroup.getEndTime()));
    }
}
